package com.caikun.shopping_car_serve.controller;


import com.caikun.model.common.Result;
import com.caikun.model.dto.ShoppingCarDTO;
import com.caikun.model.vo.ShoppingCarRecord;
import com.caikun.model.vo.ShoppingCarVO;
import com.caikun.shopping_car_serve.service.ShoppingCarService;
import com.caikun.model.entity.ShoppingCar;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车表(ShoppingCar)表控制层
 *
 * @author makejava
 * @since 2025-07-18 09:23:28
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCar")
public class ShoppingCarController {
    /**
     * 服务对象
     */
    @Autowired
    private ShoppingCarService shoppingCarService;

    /**
     * 加入或删除购物车中的商品
     * @param request 请求体参数
     * @return 操作结果
     */
    @PostMapping("/select")
    public Result<String> addOrRemoveFromCart(@RequestBody ShoppingCarDTO request) {
        log.info("request = {}", request);
        //type 1 加入购物车 0 从购物车删除 serviceType 0 服务 1 套餐
        if (request.getType() == 1) {
            if (request.getServiceType() == 0) {
                log.info("添加单个服务");
                shoppingCarService.addToShoppingCar(request.getUserId(), request.getServiceType(), request.getServiceId(), request.getPrice());
                return Result.success("加入购物车成功");
            }
            if (request.getServiceType() == 1) {
                log.info("添加套餐");
                shoppingCarService.addToShoppingCar(request.getUserId(), request.getServiceType(), request.getPackageId(), request.getPrice());
                return Result.success("加入购物车成功");
            }
        }

        if (request.getType() == 0) {
            if(request.getServiceType() == 0){
                log.info("删除单个服务");
                shoppingCarService.removeFromShoppingCar(request.getUserId(), request.getServiceType(), request.getServiceId());
                return Result.success("删除购物车成功");
            }
            if(request.getServiceType() == 1){
                log.info("删除套餐");
                shoppingCarService.removeFromShoppingCar(request.getUserId(), request.getServiceType(), request.getPackageId());
                return Result.success("删除购物车成功");
            }
        }

        return Result.success("操作成功");
    }

    /**
     * 根据 userId 删除购物车记录
     */
    @DeleteMapping("/{userId}")
    public Result<String> deleteByUserId(@PathVariable Integer userId) {
        int rows = shoppingCarService.deleteByuserId(userId);
        if (rows > 0) {
            return Result.success("删除购物车记录成功");
        } else {
            return Result.fail("未找到相关购物车记录");
        }
    }

    /**
     * 根据 userId 查询购物车记录
     */
    @GetMapping("/{userId}")
    public Result<ShoppingCarVO> getshoppingCar(@PathVariable Integer userId){
        ShoppingCarVO shoppingCars = shoppingCarService.getshoppingCar(userId);
        return Result.success(shoppingCars);
    }

    /**
     * 用于给 feign 调用查询用户的购物车记录
     * @param userId
     * @return
     */
    @GetMapping("/feign/{userId}")
    public Result<List<ShoppingCar>> getshoppingCarFeign(@PathVariable Integer userId){
        ShoppingCarVO shoppingCars = shoppingCarService.getshoppingCar(userId);

        List<ShoppingCar> shoppingCarList = new ArrayList<>();

        for (ShoppingCarRecord record : shoppingCars.getRecords()) {
            ShoppingCar shoppingCar = new ShoppingCar();
            shoppingCar.setUserId(userId);
            shoppingCar.setServiceType(record.getServiceType());
            shoppingCar.setServiceId(record.getServiceType() == 0 ? record.getShopId() : 6379);
            shoppingCar.setPackageId(record.getServiceType() == 1 ? record.getShopId() : 6379);
            shoppingCar.setCount(record.getShopcount());
            shoppingCar.setPrice(record.getShopPrice());
            shoppingCarList.add(shoppingCar);
        }

        return Result.success(shoppingCarList);
    }

}

